﻿using static RobosimAPI.Class.Account;
using System.Data.SQLite;

namespace RobosimAPI.Class
{
	public class Theme
	{
		public static List<Theme> cachedThemes=null;
		public string? Cover;
		public int Id;
		public string? Title;
		public int Type;
		public string? Version;
		public int IsNeedVip;
		/*
		cover   TEXT
		id      INTEGER
		title   TEXT
		type    INTEGER
		version TEXT
		is_need_vip INTEGER
		*/
		/*改动时注意:
		public Theme(int id)
		public static Theme[] GetAllThemes()
		*/
		public Theme(int id)
		{
			using (var conn = new SQLiteConnection(DB_CONNECTION_STRING))
			{
				conn.Open();
				var cmd = new SQLiteCommand("SELECT * FROM themeList WHERE id = @id LIMIT 1", conn);
				cmd.Parameters.AddWithValue("@id", id);
				var reader = cmd.ExecuteReader();
				if (reader.Read())
				{
					Cover=reader.GetString(0);
					Id=reader.GetInt32(1);
					Title=reader.GetString(2);
					Type=reader.GetInt32(3);
					Version=reader.GetString(4);
					IsNeedVip=reader.GetInt32(5);
				}
				conn.Close();
				conn.Dispose();
			}
		}
		Theme()
		{

		}
		public static Theme[] GetAllThemes()
		{
			if (cachedThemes == null)
			{
				cachedThemes = new List<Theme>();
				using (var conn = new SQLiteConnection(DB_CONNECTION_STRING))
				{
					conn.Open();
					var cmd = new SQLiteCommand("SELECT * FROM themeList", conn);
					var reader = cmd.ExecuteReader();
					while (reader.Read())
					{
						var theme = new Theme();
						theme.Cover = reader.GetString(0);
						theme.Id = reader.GetInt32(1);
						theme.Title = reader.GetString(2);
						theme.Type = reader.GetInt32(3);
						theme.Version = reader.GetString(4);
						theme.IsNeedVip = reader.GetInt32(5);
						cachedThemes.Add(theme);
					}
					conn.Close();
					conn.Dispose();
				}
			}
			return cachedThemes.ToArray();
		}
		public bool IsVipTheme()
		{
			return IsNeedVip == 1;
		}
	}
}
